package org.vectrics.recruiter.struts.tag;

/**
 * @author Mike Moore
 */

import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Category;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.JobCandidate;
import org.vectrics.recruiter.job.JobCandidateCriteria;
import org.vectrics.recruiter.job.JobStatusType;
import org.vectrics.struts.UserBufferManager;
import org.vectrics.struts.VectricsTagSupport;
import org.vectrics.user.Person;

public class JobCandidateSummaryTag extends VectricsTagSupport {

	private static final long serialVersionUID = 6752759922746965767L;
	
	static private Category log = Category.getInstance(JobCandidateSummaryTag.class);
    private String personId = null;
    private boolean edit = false;
	private String contextPath = null;
	private String width = "100%";
	private Person person = null;
    

	protected String getContextPath() {
		return(contextPath);
	}

	/**
	 * @return Returns the personId.
	 */
	public String getPersonId() {
		return personId;
	}
	
	/**
	 * @param personId The personId to set.
	 */
	public void setPersonId(String personId) {
		this.personId = personId;
	}
	
	
	public String makeStartTag()  {
		StringBuffer html = new StringBuffer();
		log.debug("doStartTag() called");
		Properties formProperties = new Properties();
		Properties invalidProperties = new Properties();
		HttpServletRequest request = (HttpServletRequest)super.pageContext.getRequest();
		contextPath = request.getContextPath();
		HttpSession session = super.pageContext.getSession();

		person = CoreServiceLocator.getPersonService().findPersonById(new Long(personId));
		html.append(makeCandidateTable(person));

		log.debug("Add candidate to cached objects list");
		UserBufferManager bufMgr = new UserBufferManager();
		bufMgr.addBufferedObject(this.getRequest(), person);
		
		return(html.toString());
	}

	
	
	private long getCandidateForActiveJobsCount(Collection jobCandidates) {
		long count = 0;
		Iterator iter = jobCandidates.iterator();
		while (iter.hasNext()) {
			JobCandidate jobCandidate = (JobCandidate)iter.next();
			if (jobCandidate.getJob().getCurrentStatus().getStatusType().getCode().equals(JobStatusType.STATUS_POSTED)) {
				count++;
			}
		}
		return(count);
	}

	private long getCandidateForInactiveJobsCount(Collection jobCandidates) {
		long count = 0;
		Iterator iter = jobCandidates.iterator();
		while (iter.hasNext()) {
			JobCandidate jobCandidate = (JobCandidate)iter.next();
			if (jobCandidate.getJob().getCurrentStatus().getStatusType().equals(JobStatusType.STATUS_POSTED) == false) {
				count++;
			}
		}
		return(count);
	}

	private Table makeCandidateTable(Person candidate) {
		log.debug("makeCandidateTable() called");
		int columnCount = 2;
		Table table = new BorderTable();
		table.setWidth(this.width);
		Row hdrRow = table.addRow();
        hdrRow.setDefaultBgImage(this.getContextPath() + "/site-graphics/tbl_hdr.gif");

		Cell hdrCell = hdrRow.addCell();
		hdrCell.addElement(TextHelper.getFont("tableHdrFont", getPhrase("phrase.candidate.jobs.summary")));
		//hdrCell.setClass("tableHdrBg");
		
		Row contentRow = table.addRow();
		Cell contentCell = contentRow.addCell();
		Table contentTable = new Table();
		contentCell.addElement(contentTable);
		
		JobCandidateCriteria criteria = new JobCandidateCriteria();
		criteria.setPerson(person);
		Collection jobCandidates = RecruitServiceLocator.getJobProvider().findJobCandidates(criteria, 0, 1000);
		
		log.debug("Show active jobs still candidate for");
		Row row = contentTable.addRow();
		Cell cell = row.addCell();
		cell.addElement(TextHelper.getFont("subFont", this.getPhrase("prompt.candidate.active.jobs.count") 
				+ ": " + getCandidateForActiveJobsCount(jobCandidates) ));
		
		log.debug("Show jobs not still candidate for");
		row = contentTable.addRow();
		cell = row.addCell();
		cell.addElement(TextHelper.getFont("subFont", this.getPhrase("prompt.candidate.inactive.jobs.count") 
				+ ": " + getCandidateForInactiveJobsCount(jobCandidates) ));

		return(table);
	}

	/**
	 * @return Returns the width.
	 */
	public String getWidth() {
		return width;
	}

	/**
	 * @param width The width to set.
	 */
	public void setWidth(String width) {
		this.width = width;
	}

}
